{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 02\n",
    "\n",
    "# 求余\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1567b7e-abdf-4ec0-b268-cbf7cfee4cb6",
   "metadata": {},
   "source": [
    "该代码计算两个数字之间的余数并输出结果。首先定义了两个数$7$和$2$，分别赋值给变量`num1`和`num2`，然后使用模运算符（`%`）计算余数。模运算是整数除法的一部分，它返回整数除法后的剩余部分。\n",
    "\n",
    "### 代码细节\n",
    "\n",
    "1. **余数计算**：在模运算中，$a \\mod b$的结果是$a$除以$b$后的余数。此代码计算$7 \\mod 2$，计算如下：\n",
    "\n",
    "   $$\n",
    "   \\text{remainder} = \\text{num1} \\mod \\text{num2} = 7 \\mod 2 = 1\n",
    "   $$\n",
    "\n",
    "   因为$7$除以$2$的商为$3$，余数为$1$。\n",
    "\n",
    "2. **结果显示**：代码使用格式化字符串输出结果，将计算结果表示为“`The remainder of 7 over 2 is 1`”。\n",
    "\n",
    "### 总结\n",
    "\n",
    "该代码演示了模运算的应用，计算出两个数字的余数。模运算在编程中很常见，用于确定整数的奇偶性、周期性计算以及分配问题中，是一种基本的算术运算。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd99dc60-1ad1-4c16-84f3-9d141a61171e",
   "metadata": {},
   "source": [
    "## 计算两个数字的余数并显示结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eb014ef9-72a8-4e73-972c-b150e8d5c8c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "num1 = 7  # 定义第一个数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9a0d06fe-6906-4e74-97fe-3c0cce1a7811",
   "metadata": {},
   "outputs": [],
   "source": [
    "num2 = 2  # 定义第二个数字"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96b08cd2-2067-45f6-8c21-64295a6d4dbd",
   "metadata": {},
   "source": [
    "## 计算两个数字的余数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "97437e23-1584-4315-bcd8-02cfe0a1c78b",
   "metadata": {},
   "outputs": [],
   "source": [
    "remainder = num1 % num2  # 计算num1除以num2的余数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69726a14-fe5a-4777-a89a-b5988dc0f66d",
   "metadata": {},
   "source": [
    "## 显示计算结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a075b5bd-a9d5-40dc-b3e9-c12dd16cdd68",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The remainder of 7 over 2 is 1\n"
     ]
    }
   ],
   "source": [
    "print('The remainder of {0} over {1} is {2}'.format(num1, num2, remainder))  # 输出余数结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
